package ctxdatax

import (
	"context"
	"encoding/json"

	"github.com/zeromicro/go-zero/core/logx"
)

// CtxKeyJwtUserId get uid from ctx
var CtxKeyJwtUserId = "jwtUserId"

// #24.07.29# Deprecated. Todo: 去除
func GetUidFromCtx(ctx context.Context) int64 {
	var uid int64
	if jsonUid, ok := ctx.Value(CtxKeyJwtUserId).(json.Number); ok {
		if int64Uid, err := jsonUid.Int64(); err == nil {
			uid = int64Uid
		} else {
			logx.WithContext(ctx).Errorf("GetUidFromCtx err : %+v", err)
		}
	}

	return uid
}

// #24.07.29# Deprecated. Todo: 去除
// GetuserId抽象出来，需要获取UserId尽量使用该方法，报错清晰
func GetUserId(ctx context.Context) uint64 { // #24.02.29#
	var userId uint64
	if jsonUid, ok := ctx.Value("userId").(json.Number); ok {
		if uid, err := jsonUid.Int64(); err != nil {
			logx.Errorw("GetUserId err", logx.Field("detail", err.Error()))
		} else {
			userId = uint64(uid)
		}
	}

	return userId
}
